@echo off
chcp 65001 >nul
title Cross Network Data Server - Aliyun ECS Deployment

echo ==========================================
echo   Cross Network Data Server - ECS Deploy
echo ==========================================
echo.

echo Target: Deploy WebSocket server with data persistence to Aliyun ECS
echo Features: 24/7 stable operation and cross-network data transmission
echo.

echo Preparation checklist:
echo   - cross_network_server.py (optimized with data persistence)
echo   - deploy_cross_network_server.sh (deployment script)
echo   - requirements.txt (dependencies)
echo.

echo Recommended ECS Configuration:
echo   - Instance: 2 cores 4GB memory (ecs.g6.large)
echo   - OS: Ubuntu 20.04 LTS
echo   - Bandwidth: 5Mbps+
echo   - Storage: 40GB system disk
echo   - Security Group: open ports 22, 80, 443, 8085
echo.

echo Estimated Cost: ~80-120 RMB/month
echo.

echo Starting deployment process...
echo.

set /p ecs_ip="Please enter Aliyun ECS public IP address: "
if "%ecs_ip%"=="" (
    echo ERROR: IP address cannot be empty
    pause
    exit /b 1
)

echo.
echo Step 1: Testing SSH connection...
echo Connecting to %ecs_ip%...

ssh -o ConnectTimeout=10 root@%ecs_ip% "echo 'SSH connection test successful'" 2>nul
if %errorlevel% neq 0 (
    echo ERROR: SSH connection failed
    echo.
    echo Please check:
    echo 1. ECS instance status is "Running"
    echo 2. Security group allows port 22
    echo 3. IP address is correct
    echo 4. Root password is correct
    echo.
    echo Tip: Try running test_ssh_connection.bat for detailed diagnosis
    pause
    exit /b 1
)

echo SUCCESS: SSH connection test passed
echo.

echo Step 2: Creating remote directory...
ssh root@%ecs_ip% "mkdir -p /root/cross_network_server"
if %errorlevel% neq 0 (
    echo ERROR: Failed to create directory
    pause
    exit /b 1
)
echo SUCCESS: Remote directory created

echo.
echo Step 3: Uploading server files...

echo Uploading cross_network_server.py...
scp cross_network_server.py root@%ecs_ip%:/root/cross_network_server/
if %errorlevel% neq 0 (
    echo ERROR: Failed to upload server file
    pause
    exit /b 1
)

echo Uploading deploy_cross_network_server.sh...
scp deploy_cross_network_server.sh root@%ecs_ip%:/root/cross_network_server/
if %errorlevel% neq 0 (
    echo ERROR: Failed to upload deployment script
    pause
    exit /b 1
)

echo Creating requirements.txt...
echo websockets==11.0.3> temp_requirements.txt
echo asyncio>> temp_requirements.txt
scp temp_requirements.txt root@%ecs_ip%:/root/cross_network_server/requirements.txt
del temp_requirements.txt

echo SUCCESS: All files uploaded

echo.
echo Step 4: Running automated deployment...
echo Executing deployment script on ECS server...

ssh root@%ecs_ip% "cd /root/cross_network_server && chmod +x deploy_cross_network_server.sh && bash deploy_cross_network_server.sh"

if %errorlevel% neq 0 (
    echo ERROR: Deployment script execution failed
    echo.
    echo For manual deployment, try:
    echo 1. ssh root@%ecs_ip%
    echo 2. cd /root/cross_network_server
    echo 3. bash deploy_cross_network_server.sh
    pause
    exit /b 1
)

echo.
echo Step 5: Verifying deployment results...

echo Checking service status...
ssh root@%ecs_ip% "systemctl is-active cross-network-server.service" > temp_status.txt 2>&1
set /p service_status=<temp_status.txt
del temp_status.txt

if "%service_status%"=="active" (
    echo SUCCESS: Service is running normally
) else (
    echo WARNING: Service status: %service_status%
)

echo Checking port listening...
ssh root@%ecs_ip% "netstat -tlnp | grep :8085" > nul 2>&1
if %errorlevel% equ 0 (
    echo SUCCESS: Port 8085 is listening
) else (
    echo WARNING: Port 8085 not listening, service might be starting
)

echo.
echo Step 6: Generating Android client configuration...

echo // Updated server configuration > update_server_config.kt
echo object ServerConfig { >> update_server_config.kt
echo     const val WEBSOCKET_URL = "ws://%ecs_ip%:8085" >> update_server_config.kt
echo     const val HTTP_URL = "http://%ecs_ip%:8085" >> update_server_config.kt
echo     const val CONNECTION_TIMEOUT = 30000L >> update_server_config.kt
echo     const val RECONNECT_INTERVAL = 5000L >> update_server_config.kt
echo } >> update_server_config.kt

echo SUCCESS: Client configuration file generated: update_server_config.kt

echo.
echo ==========================================
echo          DEPLOYMENT COMPLETE!
echo ==========================================
echo.
echo Deployment Information:
echo - ECS Public IP: %ecs_ip%
echo - WebSocket URL: ws://%ecs_ip%:8085
echo - Service Name: cross-network-server.service
echo - Data Directory: /root/cross_network_server/server_data
echo.
echo Management Commands:
echo - Check status: ssh root@%ecs_ip% "systemctl status cross-network-server.service"
echo - View logs: ssh root@%ecs_ip% "journalctl -u cross-network-server.service -f"
echo - Restart service: ssh root@%ecs_ip% "systemctl restart cross-network-server.service"
echo.
echo Next Steps:
echo 1. Update Android app server configuration
echo 2. Use new WebSocket address in ServerConfig.kt
echo 3. Recompile APK: .\gradlew assembleRelease
echo 4. Install and test app connection
echo.
echo Connection Testing:
echo - Run test_connection.py to test WebSocket
echo - Test real-time features in Android app
echo.
echo Data Persistence Features:
echo - Patient data auto-saved to JSON files
echo - User data auto-saved to JSON files
echo - Notification data auto-saved to JSON files
echo - Data auto-restored on service restart
echo - Daily backup at 2 AM automatically
echo.
echo Congratulations! Your cross-network data server is now running 24/7!
echo.

echo Generating deployment report...
echo Cross Network Data Server Deployment Report > CROSS_NETWORK_DEPLOYMENT_REPORT.txt
echo ================================================ >> CROSS_NETWORK_DEPLOYMENT_REPORT.txt
echo Deployment Time: %date% %time% >> CROSS_NETWORK_DEPLOYMENT_REPORT.txt
echo ECS Public IP: %ecs_ip% >> CROSS_NETWORK_DEPLOYMENT_REPORT.txt
echo WebSocket URL: ws://%ecs_ip%:8085 >> CROSS_NETWORK_DEPLOYMENT_REPORT.txt
echo Service Name: cross-network-server.service >> CROSS_NETWORK_DEPLOYMENT_REPORT.txt
echo Data Directory: /root/cross_network_server/server_data >> CROSS_NETWORK_DEPLOYMENT_REPORT.txt
echo. >> CROSS_NETWORK_DEPLOYMENT_REPORT.txt
echo Features: >> CROSS_NETWORK_DEPLOYMENT_REPORT.txt
echo - Data persistence support >> CROSS_NETWORK_DEPLOYMENT_REPORT.txt
echo - Cross-network environment adaptation >> CROSS_NETWORK_DEPLOYMENT_REPORT.txt
echo - Automatic monitoring and restart >> CROSS_NETWORK_DEPLOYMENT_REPORT.txt
echo - Scheduled data backup >> CROSS_NETWORK_DEPLOYMENT_REPORT.txt
echo - 24/7 stable operation >> CROSS_NETWORK_DEPLOYMENT_REPORT.txt

echo Deployment report saved: CROSS_NETWORK_DEPLOYMENT_REPORT.txt
echo.
pause